package com.aeromedia.testcustomapplication.recyclerVIew;

import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


import androidx.recyclerview.widget.RecyclerView;

public class CommonHolder extends RecyclerView.ViewHolder {
    private Resources resources;
    SparseArray<View> viewMap = new SparseArray<>();

    public CommonHolder(View itemView) {
        super(itemView);
        resources = itemView.getResources();
    }

    public <V extends View> V findView(int resId) {
        View view = viewMap.get(resId);
        if (view == null) {
            View findView = itemView.findViewById(resId);
            view = findView;
            viewMap.put(resId, view);
        }
        return (V) view;
    }

    public CommonHolder setText(int resId, String text) {
        TextView view = findView(resId);
        view.setText(text);
        return this;
    }


    public CommonHolder setBackGround(int resId, ChangeBackground background) {
        View view = findView(resId);
        background.change(view);
        return this;
    }

    public CommonHolder setImage(int viewId, int resId) {
        ImageView view = findView(viewId);
        view.setImageResource(resId);
        return this;
    }

    public CommonHolder setTextColor(int viewId, int color) {
        TextView view = findView(viewId);
        view.setTextColor(resources.getColor(color));
        return this;
    }

    public CommonHolder setTextDrawable(int viewId, int drawable) {
        TextView view = findView(viewId);
        Drawable leftDrawable = resources.getDrawable(drawable);
        leftDrawable.setBounds(0, 0, leftDrawable.getMinimumWidth(),
                leftDrawable.getMinimumHeight());
        view.setCompoundDrawablesRelative(leftDrawable, null, null, null);
        return this;
    }
}
